import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
import math

svc = SVC(C=3, gamma=25)

data = np.loadtxt('../data/bread.txt', delimiter=',')
m = len(data)
x = data[:, :-1]
y = data[:, -1]

np.random.seed(1)
a = np.random.permutation(m)
x = x[a]
y = y[a]

x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.7)

svc.fit(x_train, y_train)
print(svc.score(x_train, y_train))
print(svc.score(x_test, y_test))

x1_min, x1_max = np.min(x[:, 0]), np.max(x[:, 0])
x2_min, x2_max = np.min(x[:, 1]), np.max(x[:, 1])
xx, yy = np.mgrid[x1_min:x1_max:300j, x2_min:x2_max:300j]
xy = np.c_[xx.ravel(), yy.ravel()]
z = svc.decision_function(xy).reshape(xx.shape)
z11 = svc.predict(xy).reshape(xx.shape)
plt.contourf(xx, yy, z11, cmap=plt.cm.Paired)
plt.contour(xx, yy, z, colors=['w', 'r', 'g'], linestyles=['-'], levels=[-0.3, 0, 0.3])
plt.scatter(x[:, 0], x[:, 1], c=y)
plt.show()
